package com.zd.mock.liyuxian;

import com.github.javafaker.Faker;

import java.sql.*;
import java.time.LocalDateTime;
import java.util.Random;

public class ScheduleDataGenerator {
    private static final String DB_URL = "jdbc:mysql://localhost:3306/fast_food?useSSL=false&requireSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC";
    private static final String DB_USER = "root";
    private static final String DB_PASSWORD = "000000";
    private static final Faker faker = new Faker();
    private static final Random random = new Random();

    public static void main(String[] args) {
        try {
            generateSchedules(10);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void generateSchedules(int count) throws SQLException {
        String insertSql = "INSERT INTO visitor_schedules (route_id, day_number, attraction_id, duration_minutes, description, create_time) VALUES (?, ?, ?, ?, ?, ?)";

        try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
             PreparedStatement pstmt = conn.prepareStatement(insertSql)) {

            conn.setAutoCommit(false);

            for (int i = 1; i <= count; i++) {
                int routeId = random.nextInt(5) + 1;
                int dayNumber = random.nextInt(7) + 1;
                int attractionId = random.nextInt(6) + 1;
                int durationMinutes = random.nextInt(120) + 30;
                String description = faker.lorem().sentence();
                LocalDateTime createTime = LocalDateTime.now().minusDays(random.nextInt(30));

                pstmt.setInt(1, routeId);
                pstmt.setInt(2, dayNumber);
                pstmt.setInt(3, attractionId);
                pstmt.setInt(4, durationMinutes);
                pstmt.setString(5, description);
                pstmt.setTimestamp(6, Timestamp.valueOf(createTime));

                pstmt.addBatch();
                if (i % 100 == 0) {
                    pstmt.executeBatch();
                    conn.commit();
                }
            }

            pstmt.executeBatch();
            conn.commit();
            System.out.println("成功插入 " + count + " 条 visitor_schedules 数据");

        } catch (SQLException e) {
            throw e;
        }
    }
}